﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web;

namespace Doo.Extension
{
    /// <summary>
    /// 版本号管理
    /// </summary>
    public class VersionCache
    {

        #region 静态方法

        protected static Hashtable VersionTable
        {
            get
            {
                return HttpRuntime.Cache.Get("VersionTable") as Hashtable;
            }
            set
            {
                HttpRuntime.Cache["VersionTable"] = value;
            }
        }

        /// <summary>
        /// 获取版本号
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetVersion(string key)
        {
            var ht = VersionTable;
            if (ht == null) return null;
            if (ht.Contains(key)) return ht[key].ToString();
            else return null;
        }

        /// <summary>
        /// 更新为指定版本号
        /// </summary>
        /// <param name="key"></param>
        /// <param name="version"></param>
        public static void UpdateVersion(string key, string version)
        {
            var ht = VersionTable;
            if (ht == null)
            {
                ht = new Hashtable();
                VersionTable = ht;
            }
            if (ht.Contains(key))
            {
                ht[key] = version;
            }
            else
            {
                ht.Add(key, version);
            }
        }

        /// <summary>
        /// 更新版本，版本号为当前时间ticks
        /// </summary>
        /// <param name="key"></param>
        public static void UpdateVersion(string key)
        {
            UpdateVersion(key, DateTime.Now.Ticks.ToString());
        }

        /// <summary>
        /// 比较版本号，相同返回true，不同返回false
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool CompareVersion(string key, string version)
        {
            return (GetVersion(key) == version);
        }

        /// <summary>
        /// 移除版本
        /// </summary>
        /// <param name="key"></param>
        public static void RemoveVersion(string key)
        {
            if (VersionTable != null && VersionTable.ContainsKey(key))
            {
                VersionTable.Remove(key);
            }
        }

        /// <summary>
        /// 比较版本号，用ETag为旧版本号
        /// </summary>
        /// <param name="key"></param>
        /// <param name="stoponsame">如果相同则停止处理</param>
        /// <returns></returns>
        public static bool CompareRequestVersion(string key, bool stoponsame)
        {
            var etag = HttpContext.Current.Request.Headers["If-None-Match"];
            var version = GetVersion(key);
            if (version == null) return false;
            if (version != etag) return false;
            if (stoponsame)
            {
                HttpContext.Current.Response.StatusCode = 304;
                HttpContext.Current.Response.End();
            }
            return true;
        }

        /// <summary>
        /// 设置输出版本号
        /// </summary>
        /// <param name="key"></param>
        public static void SetResponseVersion(string key)
        {
            var cache = HttpContext.Current.Response.Cache;
            var version = GetVersion(key);
            if (version == null) return;
            cache.SetETag(GetVersion(key));
            cache.SetCacheability(HttpCacheability.Public);
        }

        #endregion
    }
}
